package com.das.metrocloud.ssoserver.service.impl;

import com.das.metrocloud.ssoserver.service.*;
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import com.das.cloud.system.dao.*;
import com.das.framework.common.redis.utils.*;
import com.das.cloud.system.vo.*;
import com.das.framework.common.utils.*;
import com.das.cloud.system.entity.*;
import java.util.*;

@Service
public class UserResourcesServiceImpl implements UserResourcesService
{
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SysPermissionDao sysPermissionDao;
    
    public List<Object> getCacheMenu(final String userId) {
        return (List<Object>)this.redisUtils.lGet(RedisCacheKeyUtils.userMenuKey(userId), 0L, 1000L);
    }
    
    public List<SysPermissionVO> findMyPermission(final String sysId, final String userId) {
        if (Tools.StringIsNullOrSpace(sysId) && Tools.StringIsNullOrSpace(userId)) {
            return new ArrayList<SysPermissionVO>();
        }
        return (List<SysPermissionVO>)this.sysPermissionDao.selectMyPermission(sysId, userId);
    }
    
    public List<SysPermissionEntity> findSharePermission(final String sysId) {
        return (List<SysPermissionEntity>)this.sysPermissionDao.selectSharePermission(sysId);
    }
    
    public void saveSharePermissionToRedis(final String sysId) {
        final List<SysPermissionEntity> sharePermissionList = this.findSharePermission(null);
        final Map<String, Object> permissionMaps = new HashMap<String, Object>();
        final Map<String, Object> permissionWebMaps = new HashMap<String, Object>();
        this.permissionListToMap(sharePermissionList, permissionMaps, permissionWebMaps);
        this.redisUtils.hmset("SYS_SHARE_PERMISSION", (Map)permissionMaps);
        this.redisUtils.hmset("SYS_SHARE_PERMISSION_WEB", (Map)permissionWebMaps);
    }
    
    private void permissionListToMap(final List<SysPermissionEntity> sharePermissionList, final Map<String, Object> permissionMaps, final Map<String, Object> permissionWebMaps) {
        for (final SysPermissionEntity permissionVO : sharePermissionList) {
            final String[] urls = permissionVO.getUrl().split(",");
            final String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            for (final String url : urls) {
                permissionMaps.put("/" + pre + url, permissionVO.getSysId());
            }
            if (!"N".equals(permissionVO.getCode())) {
                permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
            }
        }
    }
    
    public void saveUserPermissionToRedis(final String userId) {
        final Map<String, Object> permissionMaps = new HashMap<String, Object>();
        final Map<String, Object> permissionWebMaps = new HashMap<String, Object>();
        final List<SysPermissionVO> myPermissionList = this.findMyPermission(null, userId);
        for (final SysPermissionVO permissionVO : myPermissionList) {
            final String[] urls = permissionVO.getUrl().split(",");
            final String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            for (final String url : urls) {
                permissionMaps.put("/" + pre + url, permissionVO.getModelName() + "." + permissionVO.getName());
            }
            permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
        }
        this.redisUtils.hmset(RedisCacheKeyUtils.userPermissionKey(userId), (Map)permissionMaps);
        this.redisUtils.hmset(RedisCacheKeyUtils.userPermissionWebKey(userId), (Map)permissionMaps);
    }
}
